package demofx;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DemoFX {
    public static void main(String[] args) {
        // 窗口
        JFrame caiquan = new JFrame("猜拳");
        caiquan.setSize(700, 550);
        caiquan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        caiquan.setLocation(650, 250);
        JTextField jieguo = new JTextField();
        JLabel diannao = new JLabel("电脑");
        JLabel people = new JLabel("玩家");
        JLabel tishi = new JLabel("请先选择人要出的内容");
        JLabel jieguotishi = new JLabel("结果是:");
        JLabel bijiao = new JLabel();
        //图片
        Icon jiandaotu = new ImageIcon("img/jiandao.png");  //剪刀
        Icon shitoutu = new ImageIcon("img/shitou.png"); // 石头
        Icon butu = new ImageIcon("img/bu.png"); // 布
        Icon kongbai = new ImageIcon("img/kongbai.png");  // 空白
        // 按钮
        JButton wanjia = new JButton("玩家");
        JButton dadiannao = new JButton(kongbai);
        JButton shitou = new JButton("石头");
        JButton jiandao = new JButton("剪刀");
        JButton bu = new JButton("布");
        JButton caipan = new JButton("裁判");

        //布局
        caiquan.setLayout(null);
        //电脑和人的文本布局
        tishi.setBounds(270, 0, 150, 60);
        diannao.setBounds(70, 20, 50, 50);
        people.setBounds(490, 20, 50, 50);
        //结果提示框
        jieguo.setBounds(295, 420, 80, 20);
        jieguotishi.setBounds(250, 420, 100, 20);
        //电脑和玩家大按钮布局
        dadiannao.setBounds(30, 70, 300, 300);
        wanjia.setBounds(360, 70, 300, 300);
        // 石头剪刀布caipan 按钮布局
        shitou.setBounds(420, 400, 60, 40);
        jiandao.setBounds(480, 400, 60, 40);
        bu.setBounds(540, 400, 60, 40);
        caipan.setBounds(100, 400, 80, 40);
//        最上边提示布局
        caiquan.add(tishi);
//        电脑和玩家文本提示
        caiquan.add(diannao);
        caiquan.add(people);
//        电脑和玩家按钮
        caiquan.add(dadiannao);
        caiquan.add(wanjia);
//        石头剪刀布caipan按钮
        caiquan.add(caipan);
        caiquan.add(shitou);
        caiquan.add(jiandao);
        caiquan.add(bu);
        caiquan.setVisible(true);

// 添加响应
//电脑随机
        class MyAction implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                int suiji = (int) (Math.random() * 3 + 1);
                String a = bijiao.getText();
                if (suiji == 1) {
                    dadiannao.setIcon(jiandaotu);
                    if (a.equals("jiandao")) {
                        JOptionPane.showMessageDialog(null, "平局", "结果", JOptionPane.ERROR_MESSAGE);
                    } else if (a.equals("shitou")) {
                        JOptionPane.showMessageDialog(null, "人赢了", "结果", JOptionPane.ERROR_MESSAGE);
                    } else if (a.equals("bu")) {
                        JOptionPane.showMessageDialog(null, "电脑赢了", "结果", JOptionPane.ERROR_MESSAGE);
                    }
                }
                if (suiji == 2) {
                    dadiannao.setIcon(shitoutu);
                    if (a.equals("shitou")) {
                        JOptionPane.showMessageDialog(null, "平局", "结果", JOptionPane.ERROR_MESSAGE);
                    } else if (a.equals("bu")) {
                        JOptionPane.showMessageDialog(null, "人赢了", "结果", JOptionPane.ERROR_MESSAGE);
                    } else if (a.equals("jiandao")) {
                        JOptionPane.showMessageDialog(null, "电脑赢了", "结果", JOptionPane.ERROR_MESSAGE);
                    }
                }
                if (suiji == 3) {
                    dadiannao.setIcon(butu);
                    if (a.equals("bu")) {
                        JOptionPane.showMessageDialog(null, "平局", "结果", JOptionPane.ERROR_MESSAGE);
                    } else if (a.equals("jiandao")) {
                        JOptionPane.showMessageDialog(null, "人赢了", "结果", JOptionPane.ERROR_MESSAGE);
                    } else if (a.equals("shitou")) {
                        JOptionPane.showMessageDialog(null, "电脑赢了", "结果", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }
// 人选择
        class PeopleChoose implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent b) {
                if (b.getSource() == jiandao) {
                    wanjia.setIcon(jiandaotu);
                    bijiao.setText("jiandao");
                } else if (b.getSource() == shitou) {
                    wanjia.setIcon(shitoutu);
                    bijiao.setText("shitou");
                } else if (b.getSource() == bu) {
                    wanjia.setIcon(butu);
                    bijiao.setText("bu");
                }
            }
        }

        //        电脑随机监听
        MyAction m1 = new MyAction();
        caipan.addActionListener(m1);
        //        人选择监听
        PeopleChoose p1 = new PeopleChoose();
        jiandao.addActionListener(p1);
        shitou.addActionListener(p1);
        bu.addActionListener(p1);
    }
}
